<extend name="Public/user"/>
<block name="body">
	<!--main-->
	<section class="main_content_box">
		<div class="container">
			<div class="d_main_box">
				<div class="user_kx">
					<h3 class="user_kx_title">
						我的购物车

					</h3>
					<table class="table table-striped ">
						<thead style="background:#f5f5f5;">
						<tr class="f_fr ta_c active">
							<td class="w520">商品</td>
							<td class="w150">单价(RMB)</td>
							<td class="w150">数量</td>
							<td class="w100">小计(RMB)</td>
							<td class="w100">运费(RMB)</td>
							<td class="w100">操作</td>
						</tr>
						</thead>
						<tbody>
						<volist name="list" id="v">
							<tr style="background: #ffffff;"><th colspan="6">店铺：{$v.goodsseller} &nbsp;&nbsp;&nbsp;来源网站：{$v.shopname}</th></tr>
							<volist name="v.goods" id="g">
								<tr style="background: #ffffff;">
									<td style="width:51%;">
										<div class="fl"><a href="{$g.goodsurl}" target="_blank"><img src="{$g.goodsimg}" width="60" height="60"></a></div>
										<div class="fl" style="width:400px;font-size: 12px;padding-left: 10px;">
											<a href="{$g.goodsurl}" target="_blank" style="color:green;">{$g.goodsname}</a> <br>
											<span>尺寸：{$g.goodssize} &nbsp; 颜色：{$g.goodscolor}</span><br>
											<span>备注：{$g.goodsremark}</span> <a href="###" class="xiugai" style="display:inline-block;padding: 0px 5px;background: red;height: 16px;line-height: 16px;color:#fff;" gid="{$g.id}">修改</a>
										</div>
									</td>
									<td style="width:10%;">{$g.goodsprice}</td>
									<td style="width:11%;"><a href="javascript:void(0);" class="goodsnumjian">-</a><input type="text" name="goodsnum" class="goodsnum" value="{$g.goodsnum}" style="width: 20px;" gid="{$g.id}"><a href="javascript:void(0);" class="goodsnumjia">+</a><input type="hidden" class="maxnum" value="<?php echo $g['maxnum']?$g['maxnum']:1; ?>" name="maxnum"></td>
									<td style="width:10%;" class="xiaoji"><?php echo sprintf('%.2f',$g['goodsprice']*$g['goodsnum']) ?></td>
									<if condition="$key eq 0">
										<td style="width:10%;" rowspan="{$v.goodscount}" class="goodsfreight">{$v.max(goodsfreight)}</td>
									</if>
									<td style="width:8%"><a href="javascript:;" gid="{$g.id}" class="delcart">删除</a></td>
								</tr>
							</volist>
						</volist>
						<tr class="active">
							<td colspan="6">
								<div class="f_fr">
									<span id="totalgoodsprice">{$price.totalgoodsprice}</span> 日元 + 运费：<span id="totalgoodsfreight">{$price.totalgoodsfreight}</span> JPY <br>
									总金额：<span id="totalprice">{$price.totalprice}</span> 日元 &nbsp; 共需支付金额：<span id="zhifuprice">{$price.zhifuprice}</span> {:config('MAIN_COIN')}
								</div>
							</td>
						</tr>
						</tbody>
					</table>
					<div class="user_btn f_fr">
						<a href="{:url('Cart/checkout')}" class="btn btn-danger w150 cartbtn">去结账</a>
					</div>
				</div>
			</div>
	</section>
	<div id="xiugaibox" style="width: 200px;display: none;">
		<form action="{:url('editcart')}" method="post" id="goodsedit">
			商品尺寸：<input type="text" name="goodssize" id="goodssize" style="width: 100%;"> <br>
			商品颜色：<input type="text" name="goodscolor" id="goodscolor" style="width: 100%;"> <br>
			商品备注：<br><textarea name="goodsremark" id="goodsremark" style="width: 100%;"></textarea>
			<input type="hidden" name="id" id="goodshiddenid" value="">
			<input type="submit" value="确认修改">
		</form>
	</div>

	<script type="text/javascript">
		$(document).ready(function(){
			$('.xiugai').click(function(){
				var id=$(this).attr('gid');
				var loading=$.thinkbox.loading('{:lang("loading")}');
				$.post('{:url("getcartinfo")}',{'id':id},function(data){
					$('#goodssize').val(data.goodssize);
					$('#goodscolor').val(data.goodscolor);
					$('#goodsremark').text(data.goodsremark);
					$('#goodshiddenid').val(data.id);
					$.thinkbox('#xiugaibox',{'title':'Edit','dataEle':this,'drag':true});
					loading.hide();
				})

			})
			$('form#goodsedit').submit(function(){
				var self=$(this);
				$.post(self.attr('action'),self.serialize(),success,'json');
				return false;
				function success(data){
					if (data.status) {
						$.thinkbox.success(data.info);
						setTimeout('location.reload()',1500);
					}else{
						$.thinkbox.error(data.info);
					};
				}
			});
			$('.delcart').click(function(){
				var did=$(this).attr('gid')
				$.thinkbox.confirm('确定要删除么',{
					'dataEle' : this,
					'title':'温馨提示',
					'ok':'确认删除',
					'cancel':'关闭',
					'afterHide':function(data){
						if (data==true) {
							$.post("{:url('del')}",{'did':did},function(data){
								if (data.status) {
									$.thinkbox.success(data.info);
									setTimeout('location.reload()',1000);
								};
							})
						};
					},
				});
			})
			$('.goodsnumjian').click(function(){
				if (parseInt($(this).next('.goodsnum').val())<=1) {
					return false;
				}else{
					$(this).next('.goodsnum').val($(this).next('.goodsnum').val()-1);
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).next('.goodsnum').val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
					var id=$(this).next('.goodsnum').attr('gid');
					var num=$(this).next('.goodsnum').val();
					$.post("{:url('setnum')}",{'id':id,'num':num},function(data){

					})
				};
			})
			$('.goodsnumjia').click(function(){
				if (parseInt($(this).prev('.goodsnum').val())>=parseInt($(this).next('.maxnum').val())) {
					$.thinkbox.error('您最多能购买'+$(this).next('.maxnum').val()+'件');
					return false;
				}else{
					$(this).prev('.goodsnum').val(parseInt($(this).prev('.goodsnum').val())+1);
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).prev('.goodsnum').val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
					var id=$(this).prev('.goodsnum').attr('gid');
					var num=$(this).prev('.goodsnum').val();
					$.post("{:url('setnum')}",{'id':id,'num':num},function(data){

					})
				};
			})
			$('.goodsnum').blur(function(){
				if ($(this).val()=="") {
					$.thinkbox.error('数量不能为空');
					$(this).val('1');
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
				}else if($(this).val()>parseInt($(this).next().next('.maxnum').val())){
					$.thinkbox.error('您最多能购买'+$(this).next().next('.maxnum').val()+'件');
					$(this).val($(this).next().next('.maxnum').val());
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
				}else if($(this).val()<=0||isNaN($(this).val())){
					$.thinkbox.error('数量填写错误');
					$(this).val('1');
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
				}else{
					var danjia=$(this).parent('td').prev('td').text();
					var xiaoji=parseFloat(danjia)*parseInt($(this).val());
					$(this).parent('td').next('td').text(xiaoji.toFixed(2));
					changeprice();
				};
			})
			function changeprice(){
				var totalgoodsprice=0;
				$('.xiaoji').each(function(){
					var xiaoji=$(this).text();
					totalgoodsprice += parseFloat(xiaoji);
				})
				var totalgoodsfreight=0;
				$('.goodsfreight').each(function(){
					var freight=$(this).text();
					totalgoodsfreight += parseFloat(freight);
				})
				$('#totalgoodsfreight').text(totalgoodsfreight.toFixed(2));

				$('#totalgoodsprice').text(totalgoodsprice.toFixed(2));
				var totalprice=parseFloat(totalgoodsprice)+parseFloat($('#totalgoodsfreight').text());
				$('#totalprice').text(totalprice.toFixed(2));
				var zhifuprice=totalprice/{:config('MAIN_HUILV')};
				$('#zhifuprice').text(zhifuprice.toFixed(2));
			}
		})
	</script>

</block>